﻿using System.ComponentModel;
using WinFormsMVVM.Commands;
using WinFormsMVVM.Services;
using WinFormsMVVM.Views;

namespace WinFormsMVVM.ViewModels
{
    public class LoginViewModel : INotifyPropertyChanged
    {
        private readonly IUserService _userService;
        public event PropertyChangedEventHandler PropertyChanged;

        public string Username { get; set; }
        public string Password { get; set; }

        public ICommand<string, bool> LoginCommand { get; }

        public LoginViewModel(IUserService userService)
        {
            _userService = userService;
            LoginCommand = new RelayCommand<string, bool>(Login, null);
        }

        private bool Login(string name)
        {
            var user = _userService.GetUserByUsername(Username);
            if (user != null && user.Password == Password)
            {
                MainForm mainForm = new MainForm();
                mainForm.Show();
                return true;
            }
            else
            {
                // 显示登录失败的消息
                MessageBox.Show("用户名密码错误");
                return false;
            }
        }
    }
}
